package com.gks.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gks.entities.Blog;
import com.gks.service.BlogService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/blogs")
public class BlogController {
    @Resource
    private BlogService blogService;

    // 获取所有博客
    @GetMapping("/all")
    public List<Blog> getBlog() {
        return blogService.getAllBlogs();
    }

    // 分页获取博客
    @GetMapping
    public Page<Blog> getBlogsByPage(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return blogService.getBlogsByPage(page, size);
    }

    // 获取单个博客
    @GetMapping("/{id}")
    public Blog getBlogById(@PathVariable Long id) {
        return blogService.getBlogById(id);
    }

    // 创建博客
    @PostMapping
    public Blog createBlog(@RequestBody Blog blog) {
        return blogService.createBlog(blog);
    }

    // 更新博客
    @PutMapping("/{id}")
    public Blog updateBlog(@PathVariable Long id, @RequestBody Blog blog) {
        blog.setId(Math.toIntExact(id));
        return blogService.updateBlog(blog);
    }

    // 删除博客
    @DeleteMapping("/{id}")
    public void deleteBlog(@PathVariable Long id) {
        blogService.deleteBlog(id);
    }
}
